<?php

/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      https://github.com/CookieShop for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://www.gnu.org/licenses/gpl.html GNU GENERAL PUBLIC LICENSE
 */

namespace Inscripcion\Controller;

use Inscripcion\Controller\GenericController;
use Inscripcion\Model\Entity\MateriaCursar;
use Componentes\Controller\UtilsArrayController;
use Componentes\Controller\VariablesGlobales;

class MateriaCursarController extends GenericController {

    public function __construct() {
        
    }

    public function indexAction() {
        //busco el parametro desde el URL
        $id = $this->params()->fromRoute('id');
        echo "Index MateriaCursar  ";
        //codificar los datos otenidos a Json
        $registro_json = \Zend\Json\Json::encode($id, true);
        return $this->getResponseWithHeader($registro_json);
    }

    /**
     * Return todas las materias a cursar de un estudiante determinado
     * @param  mixed $id
     * @return mixed
     */
    public function get($id) {
        $id = $this->params()->fromRoute('id');
        $registro = $this->getMateriaCursarService()->getMateriasCursar($id);
        $data = UtilsArrayController::llenarData($registro);
        $output = array(
            'success' => true,
            'total' => count($data),
            'datos' => $data
        );
        return $this->convertirArrayAJson($output);
    }

    /**
     * Return list of resources
     * @return mixed
     */
    public function getList($id = 1, $param1 = 1, $param2 = 1, $param3 = 1) {
        $id = $this->params()->fromRoute('id');
        $param1 = $this->params()->fromRoute('param1');
        $data = null;
        $mensaje = "Defaul";
        switch ($id) {
            case (1):
                /*
                 * Listado de estudiantes segun sexo: 1=masculino y 2=femenino
                 * case = 1 y sexo = 2 
                 * http://localhost/UptaebBackend/public/maestros/materiaCursar/@metodo/@sexoid
                 * Ej: http://localhost/UptaebBackend/public/maestros/materiaCursar/1/2
                 */
                $cedula = $this->params()->fromRoute('param1');
                $registros = $this->getMateriaCursarService()->getMateriaCursarsSegunEstudiante($cedula);
                foreach ($registros as $row) {
                    $data[] = array(
                        'id_estudiante' => $row['salumid'],
                        'id_materia' => $row['smateid'],
                        'id_condicion_matricular' => $row['sconmatid'],
                    );
                }
                $output = array(
                    'success' => true,
                    'total' => count($data),
                    'data' => $data
                );
                break;
            case (2):
                /*
                 * Datos del semestre
                 * Ej: http://localhost/UptaebBackend/public/maestros/materiaCursar/2/07443224
                 * Rsp: {"success":true,"data":[{"salumid":"07443224","smateid":"HSBYC054202",
                 * "sseccid":"HS2212","slapsid":"201421","sconmatid":"0","ssemtipins":"0"}],
                 * "total":1,"mensaje":null}
                 */
                $lapsoActual = VariablesGlobales::$lapsoActual;
                $cedula = $this->params()->fromRoute('param1');
                $output = $this->getMateriaCursarService()->getMateriasCursarMenosIsncritas($cedula, $lapsoActual);
                break;
            default:
                return $param1;
        }
        //$data = UtilsArrayController::llenarData($registros);
        return $this->convertirArrayAJson($output);
    }

    /**
     * Create a new resource
     * @param  mixed $data
     * @return mixed
     */
//    public function create($data) {
//        $data = $this->params()->fromPost();
//        // Guardar el registro
//        $materiaCursar_service = new Service\MateriaCursarService($this->getServiceLocator()->get('Zend\Db\Adapter'));
//        $result = $materiaCursar_service->createRegistro($data);
//        $response = $this->getResponseWithHeader();
//        $response->setContent($result);
//        return $response;
//    }
//    public function update($id, $data) {
//        //http://guzzle.readthedocs.org/en/latest/http-messages.html
//        $id = $this->params()->fromRoute('id');
//        $json = $this->get($id);
//        echo "_----";
//        $a = $json->getBody();
//        echo $a;
//        $b = new JsonModel(array(
//            'data' => $this->get($id),
//        ));
//        echo $b;
//        echo "update ";
//        $estado = (string) $this->params()->fromRoute('param1');
//        $turno = (string) $this->params()->fromRoute('param2');
//        //$data = array('salumestado'=>$estado, 'turno'=>$turno);
//        $materiaCursar = new MateriaCursar($id, "Michell", "apellido", "sexo", "1", "ciudad", $estado, "direccion", "nacido", "05/09/1989", "lugarNacimiento", "telefono", "1", "ocupacion2", "turno2", "municipio", "2", "2");
//        // actualizar el registro
//        //echo $this->getServiceLocator()->get('Zend\Db\Adapter')->getDriver()->getDatabasePlatformName();
//        $result = $this->getMateriaCursarService()->updateRegistro($materiaCursar);
//        $response = $this->getResponseWithHeader();
//        $response->setContent($result);
//        return new JsonModel(array(
//            'data' => $this->get($id),
//        ));
//        //return $response;
//    }
//    public function replaceList($data) {
//        $id = $this->params()->fromRoute('param1');
//        echo "replaceList ";
//        $registro_json = \Zend\Json\Json::encode($id, true);
//        $response = $this->getResponseWithHeader();
//        $response->getHeaders()->addHeaders(array(
//            'Content-Type' => 'application/json',)
//        );
//        $response->setContent($registro_json);
//        return $response;
//    }

    /**
     * Delete an existing resource
     * @param  mixed $id
     * @return mixed
     */
//    public function delete($id) {
//        // obtener el id del registro
//        $id = $this->params()->fromRoute('param1');
//        // Eliminar el registro
//        $materiaCursar_service = Service\MateriaCursarService::getInstance();
//        $result = $materiaCursar_service->deleteRegistro($id);
//        $response = $this->getResponseWithHeader();
//        $response->setContent($result);
//        return $response;
//    }
}
